package state.component;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import state.GameConstants;
import state.GameForm;
import state.Manager;
import state.Sprite;
import state.util.ImageUtil;

public class MultiTab extends Component {
  public static final String OPEN_TAB_EVENT = "openTab";
  
	private Vector titles = new Vector(); // String
	private Vector tabs = new Vector(); // GameForm
	
	private int headerHeight = 20;
	private int currentSelectedTab = 0;
	private boolean isSelectedTabRepeat = false;
	
	private Image tabImage;
	private Image tabFocusImage;
	
	public MultiTab(Manager manager, int width, int height) {
	  super(manager, width, height);
	  tabImage = ImageUtil.getCoreImage("tab.png");
	  tabFocusImage = ImageUtil.getCoreImage("tab_focus.png");
	  setFocusable(true);
	  setFocused(true);
	}
	
	public int addTab(String title, GameForm tab) {
	  if (title == null || tab == null) {
	    throw new IllegalArgumentException();
	  }
	  titles.addElement(title);
	  tabs.addElement(tab);
	  tab.init(null);
	  return tabs.size() - 1;
	}
	
	public void removeTab(int index) {
	  if (index > tabs.size() - 1) {
	    return;
	  }
	  titles.removeElementAt(index);
	  tabs.removeElementAt(index);
	  
	  if (currentSelectedTab > tabs.size() - 1) {
	    currentSelectedTab = tabs.size() - 1;
	  }
	}
	
	public int getHeaderHeight() {
    return headerHeight;
  }

  public void setHeaderHeight(int headerHeight) {
    this.headerHeight = headerHeight;
  }
  
  public int getCurrentSelectedTab() {
    return currentSelectedTab;
  }

  public void setCurrentSelectedTab(int currentSelectedTab) {
    this.currentSelectedTab = currentSelectedTab;
  }
  
  public boolean isSelectedTabRepeat() {
    return isSelectedTabRepeat;
  }

  public void setSelectedTabRepeat(boolean isSelectedTabRepeat) {
    this.isSelectedTabRepeat = isSelectedTabRepeat;
  }
  
  public GameForm getCurrentTab() {
    return (GameForm) tabs.elementAt(currentSelectedTab);
  }

  public void nextTab() {
    if (isSelectedTabRepeat) {
      currentSelectedTab = (currentSelectedTab + 1) % tabs.size();
    } else {
      currentSelectedTab = ++currentSelectedTab > (tabs.size() - 1) ? (tabs.size() - 1) : currentSelectedTab;
    }
    callBackEvent(tabs.elementAt(currentSelectedTab), OPEN_TAB_EVENT);
  }
  
  public void previousTab() {
    if (isSelectedTabRepeat) {
      currentSelectedTab = --currentSelectedTab < 0 ? (tabs.size() - 1) : currentSelectedTab;
    } else {
      currentSelectedTab = --currentSelectedTab < 0 ? 0 : currentSelectedTab;
    }
    callBackEvent(tabs.elementAt(currentSelectedTab), OPEN_TAB_EVENT);
  }
  
  public void selectTab(int index) {
    if (index < tabs.size()) {
      currentSelectedTab = index;
      callBackEvent(tabs.elementAt(currentSelectedTab), OPEN_TAB_EVENT);
    }
  }
  
  private void callBackEvent(Object tab, String action) {
    if (tab instanceof EventListener) {
      EventListener listener = (EventListener) tab;
      Event event = new Event(tab, action);
      listener.onActionPerform(event);
    }
  }

  public void draw(Graphics g) {
    // Draw tab header
    int tabWidth = width / titles.size();
    if (tabWidth > tabImage.getWidth()) {
      tabWidth = tabImage.getWidth();
    }
    for (int i = 0; i < titles.size(); i++) {
      String title = String.valueOf(titles.elementAt(i));
      if (i == currentSelectedTab) {
        g.drawImage(tabFocusImage, realX + i * tabWidth, realY, GameConstants.TOP_LEFT_ANCHOR);
        getFont().drawString(g, title, focusFgColor, realX + i * tabWidth + 2, realY + 2, GameConstants.TOP_LEFT_ANCHOR);
      } else {
        g.drawImage(tabImage, realX + i * tabWidth, realY, GameConstants.TOP_LEFT_ANCHOR);
        getFont().drawString(g, title,  forceGroundColor, realX + i * tabWidth + 2, realY + 2, GameConstants.TOP_LEFT_ANCHOR);
      }
    }
    
    // Draw background
    if (isFillBackGround) {
      g.setColor(backgroundColor);
      g.fillRect(realX, realY + headerHeight, width, height - headerHeight);
    }
    
    // Draw border
    if (hasBorder) {
      g.setColor(borderColor);
      g.drawRect(realX, realY + headerHeight, width, height - headerHeight);
    }
    
    // Draw current selected tab content
    g.setClip(realX, realY + headerHeight, width, height - headerHeight);
    GameForm form = (GameForm) tabs.elementAt(currentSelectedTab);
    form.draw(g);
    g.setClip(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
	}

  public void keyPressed(Sprite source, int keyCode) {
    if (focusable && isFocused) {
      if (tabs.size() > 0) {
        ((GameForm) tabs.elementAt(currentSelectedTab)).doKeyPressed(keyCode);
      }
    }
  }

  public void keyReleased(Sprite source, int keyCode) {
     if (focusable && isFocused) {
       if (tabs.size() > 0) {
         ((GameForm) tabs.elementAt(currentSelectedTab)).doKeyReleased(keyCode);
       }
     }
  }

  public void keyRepeated(Sprite source, int keyCode) {
    if (focusable && isFocused) {
      if (tabs.size() > 0) {
        ((GameForm) tabs.elementAt(currentSelectedTab)).doKeyRepeated(keyCode);
      }
    }
  }

  public Sprite detroy() {
    super.detroy();
    tabImage = null;
    tabFocusImage = null;
    titles = null;
    tabs = null;
    return this;
  }
}
